﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CryptUtil;
using Server.Util;
using Newtonsoft.Json;
using Server.Models;
using System.Data.SqlClient;
using System.Data;

namespace Server.ManagerService
{
    public class ManagerDAL
    {
        CacheUtil cacheCtrl = new CacheUtil();
        DBUtil dbCtrl = new DBUtil();
        public bool CheckManager(string Token)
        {
            try
            {
                string tmpStr = (string)cacheCtrl.CacheGet("Manage_Online:Token:" + Token);
                if (string.IsNullOrEmpty(tmpStr))
                    return false;
                tmpStr = JsonConvert.DeserializeObject<string>(tmpStr);
                if (string.IsNullOrEmpty(tmpStr))
                    return false;
                return true;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ManagerModel GetManagerByToken(string Token)
        {
            try
            {
                string tmpStr = (string)cacheCtrl.CacheGet("Manage_Online:Token:" + Token);
                if (string.IsNullOrEmpty(tmpStr))
                    throw new Exception("用户已掉线,请重新登录!");
                tmpStr = JsonConvert.DeserializeObject<string>(tmpStr);
                return this.GetManagerByID(Convert.ToInt32(tmpStr));
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ManagerModel GetManagerByID(int id)
        {
            try
            {
                string tmpStr = (string)cacheCtrl.CacheGet("Manage_Manager:ID:" + id);
                if (string.IsNullOrEmpty(tmpStr))
                {
                    CacheManagerByID();
                    tmpStr = (string)cacheCtrl.CacheGet("Manage_Manager:ID:" + id);
                }
                ManagerModel manager = JsonConvert.DeserializeObject<ManagerModel>(tmpStr);
                return manager;
            }
            catch (Exception)
            {
                throw;
            }

        }
        public ManagerModel GetManagerByName(string LoginName)
        {
            try
            {
                string tmpStr = (string)cacheCtrl.CacheGet("Manage_Manager:LoginName:" + LoginName);
                if (string.IsNullOrEmpty(tmpStr))
                {
                    CacheManagerByName();
                    tmpStr = (string)cacheCtrl.CacheGet("Manage_Manager:LoginName:" + LoginName);
                }
                ManagerModel manager = JsonConvert.DeserializeObject<ManagerModel>(tmpStr);
                return manager;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SaveOnline(string Token, int id)
        {
            try
            {
                cacheCtrl.CacheSet("Manage_Online:Token:" + Token, id, 60);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 管理员退出
        /// </summary>
        /// <param name="Token"></param>
        public void ManagerLogout(string Token)
        {
            try
            {
                cacheCtrl.CacheSet("Manage_Online:Token:" + Token, "", 1);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="manager"></param>
        public void ManagerAdd(ManagerModel manager)
        {
            try
            {
                string sqlstr = " insert into Manage_Manager(MLoginName,MPassWord,Sault,MName,MCompanyID,State) values(@MLoginName,@MPassWord,@Sault,@MName,@MCompanyID,@State) ";
                SqlParameter[] op = new SqlParameter[]
                {
                    new SqlParameter { SqlDbType = SqlDbType.VarChar,ParameterName = "@MLoginName",Value = manager.MLoginName},
                    new SqlParameter { SqlDbType = SqlDbType.VarChar ,ParameterName = "@MPassWord",Value = manager.MPassWord},
                    new SqlParameter { SqlDbType = SqlDbType.VarChar ,ParameterName = "@Sault",Value = manager.Sault},
                    new SqlParameter { SqlDbType = SqlDbType.VarChar ,ParameterName = "@MName",Value = manager.MName},
                    new SqlParameter { SqlDbType = SqlDbType.Int ,ParameterName = "@MCompanyID",Value = manager.MCompanyID},
                    new SqlParameter { SqlDbType = SqlDbType.Int ,ParameterName = "@State",Value = manager.State},
                };
                dbCtrl.Exec(sqlstr, op);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="manger"></param>
        public void PassEdit(ManagerModel manager)
        {
            try
            {
                string sqlstr = string.Format(" update Manage_Manager set MPassWord = '{0}' where id = {1} ", manager.MPassWord, manager.id);
                dbCtrl.Exec(sqlstr);

            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 修改个人信息
        /// </summary>
        /// <param name="manager"></param>
        public void ManagerEdit(ManagerModel manager)
        {
            try
            {
                string sqlstr = " update Manage_Manager set MName = @MName, MCompanyID = @MCompanyID where id = @id";
                SqlParameter[] op = new SqlParameter[]
                {
                    new SqlParameter{SqlDbType = SqlDbType.VarChar, ParameterName = "@MName",Value = manager.MName},
                    new SqlParameter{SqlDbType = SqlDbType.Int,ParameterName = "@MCompanyID",Value = manager.MCompanyID},
                    new SqlParameter{SqlDbType = SqlDbType.Int,ParameterName ="@id",Value = manager.id}
                };
                dbCtrl.Exec(sqlstr, op);
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id"></param>
        public void ManagerState(int id)
        {
            try
            {
                string sqlstr = " update Manage_Manager set State = ABS(State-1) where id = " + id;
                dbCtrl.Exec(sqlstr);
                CacheManagerByID();
                CacheManagerByName();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ManagerModel[] GetManagerList()
        {
            try
            {
                string sqlstr = " select * from Manage_Manager ";
                var dt = dbCtrl.Query(sqlstr);
                ManagerModel[] manager = new ManagerModel[dt.Rows.Count];
                for (int i = 0; i < manager.Length; i++)
                {
                    var dr = dt.Rows[i];
                    manager[i] = new ManagerModel
                    {
                        id = Convert.ToInt32(dr["id"]),
                        MCompanyID = Convert.ToInt32(dr["MCompanyID"]),
                        MLoginName = dr["MLoginName"].ToString(),
                        MName = dr["MName"].ToString(),
                        State = Convert.ToInt32(dr["State"])
                    };
                }
                return manager;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 缓存ID用户信息
        /// </summary>
        public void CacheManagerByID()
        {
            try
            {
                string sqlstr = " select * from Manage_Manager ";
                var dt = dbCtrl.Query(sqlstr);
                ManagerModel manager = new ManagerModel();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    manager = new ManagerModel()
                    {
                        id = Convert.ToInt32(dr["id"]),
                        MLoginName = dr["MLoginName"].ToString(),
                        MPassWord = dr["MPassWord"].ToString(),
                        Sault = dr["Sault"].ToString(),
                        MName = dr["MName"].ToString(),
                        MCompanyID = Convert.ToInt32(dr["MCompanyID"]),
                        State = Convert.ToInt32(dr["State"])
                    };
                    cacheCtrl.CacheSet("Manage_Manager:ID:" + dr["id"], manager);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CacheManagerByName()
        {
            try
            {
                string sqlstr = " select * from Manage_Manager ";
                var dt = dbCtrl.Query(sqlstr);
                ManagerModel manager = new ManagerModel();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    manager = new ManagerModel()
                    {
                        id = Convert.ToInt32( dr["id"]),
                        MLoginName = dr["MLoginName"].ToString(),
                        MPassWord = dr["MPassWord"].ToString(),
                        Sault = dr["Sault"].ToString(),
                        MName = dr["MName"].ToString(),
                        MCompanyID = Convert.ToInt32(dr["MCompanyID"]),
                        State = Convert.ToInt32(dr["State"])
                    };
                    cacheCtrl.CacheSet("Manage_Manager:LoginName:" +dr["MLoginName"],manager);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
